package com.LeeCode;

/**
 * 爱吃香蕉的珂珂
 */

public class Code875 {
    public static void main(String[] args) {
        int[] piles = {30, 11, 23, 4, 20};
        int h = 5;
        System.out.println(new Code875().minEatingSpeed(piles, h));
    }

    public int minEatingSpeed(int[] piles, int h) {
        int left = 0, right = Integer.MAX_VALUE;
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (check(piles, mid, h)) {
                right = mid;
            } else {
                left = mid;
            }
        }
        return right;
    }

    private boolean check(int[] piles, int speed, int h) {
        int totol = 0;
        for (int pile : piles) {
            totol += (pile + speed - 1) / speed;
            if (totol > h)
                return false;
        }
        return true;
    }
}
